package com.knowledge.action;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

import com.knowledge.DAO.DBpool;
import com.knowledge.DAO.KnowledgeDAO.DataSource;
import com.knowledge.model.Comment;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ViewAction extends ActionSupport {
	private int knowledgeId;
	List<Comment> commentList = new ArrayList<Comment>();
	private String userId;
	private String title = null;
	private String content = null;
	private String username = null;
	private String addtime = null;
	private String user = null;

	
	
	public List<Comment> getCommentList() {
		return commentList;
	}

	public void setCommentList(List<Comment> commentList) {
		this.commentList = commentList;
	}

	public int getKnowledgeId() {
		return knowledgeId;
	}

	public void setKnowledgeId(int knowledgeId) {
		this.knowledgeId = knowledgeId;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getAddtime() {
		return addtime;
	}

	public void setAddtime(String addtime) {
		this.addtime = addtime;
	}

	public String getUser() {
		return user;
	}

	public void setUser(String user) {
		this.user = user;
	}
	

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Override
	public String execute() throws Exception {

		Connection con = DBpool.getConnection();

		String sql = "select title, content, addtime " + "from knowledge "
				+ "where id=? ";
		PreparedStatement prepstmt = null;
		ResultSet rs = null;
		try {
			prepstmt = con.prepareStatement(sql);
			prepstmt.setInt(1, knowledgeId);
			rs = prepstmt.executeQuery();
			while (rs.next()) {
				this.title = rs.getString(1);
				this.content = rs.getString(2);
				this.addtime = rs.getString(3);
			}

			con.close();
			prepstmt.close();
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.setCommentList(DataSource.queryCommentList(String.valueOf(knowledgeId)));
		return "success";
	}

}
